package cn.iocoder.yudao.module.product.controller.admin.fixprice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.fixprice.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.fixprice.FixPriceDO;
import cn.iocoder.yudao.module.product.service.fixprice.FixPriceService;

@Tag(name = "管理后台 - 商品定价")
@RestController
@RequestMapping("/product/fix-price")
@Validated
public class FixPriceController {

    @Resource
    private FixPriceService fixPriceService;

    @PostMapping("/create")
    @Operation(summary = "创建商品定价")
    @PreAuthorize("@ss.hasPermission('product:fix-price:create')")
    public CommonResult<Integer> createFixPrice(@Valid @RequestBody FixPriceSaveReqVO createReqVO) {
        return success(fixPriceService.createFixPrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品定价")
    @PreAuthorize("@ss.hasPermission('product:fix-price:update')")
    public CommonResult<Boolean> updateFixPrice(@Valid @RequestBody FixPriceSaveReqVO updateReqVO) {
        fixPriceService.updateFixPrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品定价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:fix-price:delete')")
    public CommonResult<Boolean> deleteFixPrice(@RequestParam("id") Integer id) {
        fixPriceService.deleteFixPrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品定价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:fix-price:query')")
    public CommonResult<FixPriceRespVO> getFixPrice(@RequestParam("id") Integer id) {
        FixPriceDO fixPrice = fixPriceService.getFixPrice(id);
        return success(BeanUtils.toBean(fixPrice, FixPriceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品定价分页")
    @PreAuthorize("@ss.hasPermission('product:fix-price:query')")
    public CommonResult<PageResult<FixPriceRespVO>> getFixPricePage(@Valid FixPricePageReqVO pageReqVO) {
        PageResult<FixPriceDO> pageResult = fixPriceService.getFixPricePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FixPriceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品定价 Excel")
    @PreAuthorize("@ss.hasPermission('product:fix-price:export')")
    @OperateLog(type = EXPORT)
    public void exportFixPriceExcel(@Valid FixPricePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FixPriceDO> list = fixPriceService.getFixPricePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品定价.xls", "数据", FixPriceRespVO.class,
                        BeanUtils.toBean(list, FixPriceRespVO.class));
    }

}